package com.wy17113.view.struts.action;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.wy17113.controller.utils.Page;
import com.wy17113.model.entity.Common;
import com.wy17113.model.entity.LifeInfo;
import com.wy17113.model.service.ILifeInfoService;

public class LifeInfoAction extends DispatchAction {	
	
	//后台（批量）删除信息
	public ActionForward deleteCommon(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String respone = "";
		
		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
		.getRequiredWebApplicationContext(application);
		ILifeInfoService lifeInfos = (ILifeInfoService) webContext.getBean("lifeInfoService");
		
		String[] ids = request.getParameterValues("commonIDList");
			
		try {
			lifeInfos.deleteCommons(ids);
			respone = "del_com_ok";
		} catch (Exception e) {
			respone = "error";
		}						
		return mapping.findForward(respone);		
	}	
	
	//根据id获取评论
	public ActionForward getCommonListByInfoId(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String respone = "";
		HttpSession session = request.getSession(true);
		String infoId = request.getParameter("infoId");
		String pageNumStr = request.getParameter("pageNum");
		
		String flag = request.getParameter("flag");
		
		if (pageNumStr == null)
			pageNumStr = "1";								
		int pageNum = Integer.parseInt(pageNumStr);

		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
		.getRequiredWebApplicationContext(application);
		ILifeInfoService lifeInfos = (ILifeInfoService) webContext.getBean("lifeInfoService");
		
		String pageStr = "";
		List<Common> allCommons = null;
		int allCounts = 0;		
		LifeInfo lifeInfo = null;
		
		try {			
			allCounts = lifeInfos.getCommonCountByInfoId(infoId);
			
			Page page = new Page(pageNum,30, allCounts);
			pageStr = Page.getPageString("getLifeInfoList", page.getCurrentPageIndex(), page.getPageCount());
						
			allCommons = lifeInfos.getCommonListByInfoId(infoId, page.getStartIndex(), page.getPageRowCount());
			
			if(null != infoId && !"".equals(infoId)){
				lifeInfo = lifeInfos.getLifeInfoById(infoId);
			}			
			
			if(null != flag && "1".equals(flag)){
				respone = "toDiaoSiCommonShenHe";
				session.setAttribute("infoId", infoId);
			}else{
				lifeInfos.updateClicksById(infoId);
				respone = "toDiaoSiDetail";	
			}
			
		} catch (RuntimeException e) {
			respone = "error";
			e.printStackTrace();
		}
		
		request.setAttribute("allCounts", allCounts);
		request.setAttribute("allCommons", allCommons);
		request.setAttribute("pageStr", pageStr);
		request.setAttribute("infoId", infoId);
		request.setAttribute("lifeInfo", lifeInfo);		
		
		return mapping.findForward(respone);				
	}
	
	//根据类别编号获取相应的生活信息
	public ActionForward getLifeInfoList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String respone = "";
		String typeId = request.getParameter("typeId");
		String flag = request.getParameter("flag");//标记是否为后台方法
		String pageNumStr = request.getParameter("pageNum");
				
		if (pageNumStr == null)
			pageNumStr = "1";								
		int pageNum = Integer.parseInt(pageNumStr);

		if (flag == null)
			flag = "2";
		
		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
		.getRequiredWebApplicationContext(application);
		ILifeInfoService lifeInfos = (ILifeInfoService) webContext.getBean("lifeInfoService");
		
		String pageStr = "";
		List<LifeInfo> allLifeInfo = null;
		int allCounts = 0;		
		Page page = null;
		try {			
			allCounts = lifeInfos.getLifeInfoListCounts(Integer.parseInt(typeId),null,flag);
			
			page = new Page(pageNum, allCounts);
			pageStr = Page.getPageString("getLifeInfoList", page.getCurrentPageIndex(), page.getPageCount());
						
			allLifeInfo = lifeInfos.getLifeInfoList(Integer.parseInt(typeId),null,page.getStartIndex(), page.getPageRowCount(),flag);
			
			if("0".equals(flag) || "1".equals(flag) ){//后台方法
				respone = "toGetAllLifeInfoList";				
			}else{
				if(typeId.equals("9")){
					respone = "toGetNewsList";
				}else if(typeId.equals("7")){
					respone = "toGetDiaoSiList";
				}else{
					respone = "toGetLifeInfoList";
				}
			}								
		} catch (RuntimeException e) {
			respone = "error";
			e.printStackTrace();
		}
		
		request.setAttribute("allCounts", allCounts);
		request.setAttribute("allLifeInfo", allLifeInfo);
		request.setAttribute("pageStr", pageStr);
		request.setAttribute("typeId", typeId);
		request.setAttribute("flag", flag);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("pageCount", page.getPageCount());
				
		return mapping.findForward(respone);		
	}	
	
	//添加新闻
	public ActionForward addNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String respone = "";
		SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
					
		String content = request.getParameter("Content");
		String location = request.getParameter("location");
		
		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
		.getRequiredWebApplicationContext(application);
		ILifeInfoService lifeInfos = (ILifeInfoService) webContext.getBean("lifeInfoService");
		
		LifeInfo lifeInfo = new LifeInfo();
		
		lifeInfo.setName("新闻");
		lifeInfo.setContent(content);
		lifeInfo.setTypeId(new Integer(9));
		lifeInfo.setKindId(new Integer(0));
		lifeInfo.setLocation("");
		lifeInfo.setSort(lifeInfos.getMaxSort() + 1);		
		lifeInfo.setMemo(location);
		lifeInfo.setPrice("0");
		lifeInfo.setUserId("");
		lifeInfo.setState(new Integer(1));
		lifeInfo.setSubDate(smt.format(new Date()));
		
		try {
			lifeInfos.saveLifeInfo(lifeInfo);
			respone = "add_ok";
		} catch (SQLException e) {
			respone = "error";
		}						
		return mapping.findForward(respone);		
	}	
	
	//后台（批量）删除信息
	public ActionForward deleteLifeInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String respone = "";
		
		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
		.getRequiredWebApplicationContext(application);
		ILifeInfoService lifeInfos = (ILifeInfoService) webContext.getBean("lifeInfoService");
		
		String[] ids = request.getParameterValues("lifeInfoIDList");
			
		try {
			lifeInfos.deleteLifeInfos(ids);
			respone = "del_ok";
		} catch (Exception e) {
			respone = "error";
		}						
		return mapping.findForward(respone);		
	}	
	
	//后台（批量）审核信息
	public ActionForward shenHeLifeInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String respone = "";
		
		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
		.getRequiredWebApplicationContext(application);
		ILifeInfoService lifeInfos = (ILifeInfoService) webContext.getBean("lifeInfoService");
		
		String[] ids = request.getParameterValues("lifeInfoIDList");
			
		try {
			lifeInfos.shenHeLifeInfos(ids);
			respone = "del_ok";
		} catch (Exception e) {
			respone = "error";
		}						
		return mapping.findForward(respone);		
	}	
	
	//置顶 信息
	public ActionForward zhiDingLifeInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String respone = "";
		
		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
		.getRequiredWebApplicationContext(application);
		ILifeInfoService lifeInfos = (ILifeInfoService) webContext.getBean("lifeInfoService");
		
		String id = request.getParameter("id");
			
		try {
			lifeInfos.zhiDingLifeInfo(id);
			respone = "del_ok";
		} catch (Exception e) {
			respone = "error";
		}						
		return mapping.findForward(respone);		
	}
	
	//发布 招聘会
	public ActionForward addZhaoPinHui(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String respone = "";
		SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
					
		String content = request.getParameter("Content");
		String userId = request.getParameter("userId");
		String location = request.getParameter("location");
		String memo = request.getParameter("memo");
		
		ServletContext application = request.getSession().getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils
		.getRequiredWebApplicationContext(application);
		ILifeInfoService lifeInfos = (ILifeInfoService) webContext.getBean("lifeInfoService");
		
		LifeInfo lifeInfo = new LifeInfo();
		
		lifeInfo.setName("招聘会");
		lifeInfo.setContent(content);
		lifeInfo.setTypeId(new Integer(6));
		lifeInfo.setKindId(new Integer(0));
		lifeInfo.setLocation(location);
		lifeInfo.setMemo(memo);
		lifeInfo.setSort(lifeInfos.getMaxSort() + 1);
		lifeInfo.setPrice("0");
		lifeInfo.setUserId(userId);
		lifeInfo.setState(new Integer(1));
		lifeInfo.setSubDate(smt.format(new Date()));
		
		try {
			lifeInfos.saveLifeInfo(lifeInfo);
			respone = "add_zhaopin_ok";
		} catch (SQLException e) {
			respone = "error";
		}						
		return mapping.findForward(respone);		
	}	
}
